<?php
/**
 * 完整系统测试文件
 * 测试CDK支付系统的所有功能
 */

require_once 'classes/Database.php';
require_once 'classes/ProductManager.php';
require_once 'classes/CDKManager.php';
require_once 'classes/OrderManager.php';
require_once 'classes/EasyPay.php';
require_once 'classes/SettingsHelper.php';

echo "<!DOCTYPE html>
<html lang='zh-CN'>
<head>
    <meta charset='UTF-8'>
    <meta name='viewport' content='width=device-width, initial-scale=1.0'>
    <title>CDK支付系统 - 完整测试</title>
    <link href='https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css' rel='stylesheet'>
    <style>
        .test-section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #dee2e6;
            border-radius: 5px;
        }
        .test-success {
            background-color: #d4edda;
            border-color: #c3e6cb;
        }
        .test-error {
            background-color: #f8d7da;
            border-color: #f5c6cb;
        }
        .test-warning {
            background-color: #fff3cd;
            border-color: #ffeaa7;
        }
        .test-info {
            background-color: #d1ecf1;
            border-color: #bee5eb;
        }
        .test-result {
            margin-top: 10px;
            padding: 10px;
            border-radius: 3px;
        }
    </style>
</head>
<body>
    <div class='container mt-4'>
        <h1 class='text-center mb-4'>CDK支付系统 - 完整功能测试</h1>
        
        <div class='alert alert-info'>
            <h4><i class='fas fa-info-circle'></i> 测试说明</h4>
            <p>此页面将测试系统的所有核心功能，包括数据库连接、商品管理、CDK管理、订单管理和支付功能。</p>
        </div>";

// 测试数据库连接
echo "<div class='test-section test-info'>
        <h3><i class='fas fa-database'></i> 1. 数据库连接测试</h3>";

try {
    $db = Database::getInstance();
    echo "<div class='test-result test-success'>
            <i class='fas fa-check-circle'></i> 数据库连接成功
        </div>";
} catch (Exception $e) {
    echo "<div class='test-result test-error'>
            <i class='fas fa-times-circle'></i> 数据库连接失败: " . $e->getMessage() . "
        </div>";
}

echo "</div>";

// 测试商品管理
echo "<div class='test-section test-info'>
        <h3><i class='fas fa-box'></i> 2. 商品管理测试</h3>";

try {
    $productManager = new ProductManager();
    
    // 测试获取商品列表
    $products = $productManager->getAllProducts(true);
    echo "<div class='test-result test-success'>
            <i class='fas fa-check-circle'></i> 获取商品列表成功，共 " . count($products) . " 个商品
        </div>";
    
    // 测试创建商品
    $testProduct = [
        'name' => '测试商品 - ' . date('Y-m-d H:i:s'),
        'description' => '这是一个测试商品',
        'price' => 10.00,
        'stock' => 100,
        'status' => 'active'
    ];
    
    $productId = $productManager->createProduct($testProduct);
    if ($productId) {
        echo "<div class='test-result test-success'>
                <i class='fas fa-check-circle'></i> 创建测试商品成功，ID: " . $productId . "
            </div>";
        
        // 测试获取单个商品
        $product = $productManager->getProductById($productId);
        if ($product) {
            echo "<div class='test-result test-success'>
                    <i class='fas fa-check-circle'></i> 获取商品详情成功: " . htmlspecialchars($product['name']) . "
                </div>";
        }
        
        // 测试更新商品
        $updateData = ['price' => 15.00];
        if ($productManager->updateProduct($productId, $updateData)) {
            echo "<div class='test-result test-success'>
                    <i class='fas fa-check-circle'></i> 更新商品价格成功
                </div>";
        }
        
        // 测试删除商品
        if ($productManager->deleteProduct($productId)) {
            echo "<div class='test-result test-success'>
                    <i class='fas fa-check-circle'></i> 删除测试商品成功
                </div>";
        }
    } else {
        echo "<div class='test-result test-error'>
                <i class='fas fa-times-circle'></i> 创建测试商品失败
            </div>";
    }
    
} catch (Exception $e) {
    echo "<div class='test-result test-error'>
            <i class='fas fa-times-circle'></i> 商品管理测试失败: " . $e->getMessage() . "
        </div>";
}

echo "</div>";

// 测试CDK管理
echo "<div class='test-section test-info'>
        <h3><i class='fas fa-gift'></i> 3. CDK管理测试</h3>";

try {
    $cdkManager = new CDKManager();
    $productManager = new ProductManager();
    
    // 获取一个商品用于测试
    $products = $productManager->getAllProducts(true);
    if (empty($products)) {
        echo "<div class='test-result test-warning'>
                <i class='fas fa-exclamation-triangle'></i> 没有可用商品，跳过CDK测试
            </div>";
    } else {
        $testProduct = $products[0];
        
        // 测试生成CDK
        $cdkCode = $cdkManager->generateCDK();
        if ($cdkCode) {
            echo "<div class='test-result test-success'>
                    <i class='fas fa-check-circle'></i> 生成CDK成功: " . htmlspecialchars($cdkCode) . "
                </div>";
            
            // 测试添加CDK
            if ($cdkManager->addCDK($testProduct['id'], $cdkCode, 'available')) {
                echo "<div class='test-result test-success'>
                        <i class='fas fa-check-circle'></i> 添加CDK到商品成功
                    </div>";
            }
            
            // 测试批量生成CDK
            $batchCDKs = $cdkManager->batchGenerateCDK(3, $testProduct['id'], 16);
            if ($batchCDKs) {
                echo "<div class='test-result test-success'>
                        <i class='fas fa-check-circle'></i> 批量生成CDK成功，生成 " . count($batchCDKs) . " 个CDK
                    </div>";
            }
            
            // 测试获取CDK列表
            $cdks = $cdkManager->getCDKsByProduct($testProduct['id']);
            echo "<div class='test-result test-success'>
                    <i class='fas fa-check-circle'></i> 获取CDK列表成功，共 " . count($cdks) . " 个CDK
                </div>";
            
            // 测试使用CDK
            if (!empty($cdks)) {
                $cdkToUse = $cdks[0];
                // 创建一个测试订单用于使用CDK
                $orderManager = new OrderManager();
                $testOrderResult = $orderManager->createOrder($testProduct['id'], $testProduct['price'], 'test@example.com', 'test', 0);
                if ($testOrderResult && isset($testOrderResult['id'])) {
                    $testOrderId = $testOrderResult['id'];
                    if ($cdkManager->useCDK($cdkToUse['id'], $testOrderId)) {
                        echo "<div class='test-result test-success'>
                                <i class='fas fa-check-circle'></i> 使用CDK成功: " . htmlspecialchars($cdkToUse['cdk_code']) . "
                            </div>";
                    }
                }
            }
            
            // 测试CDK统计
            $stats = $cdkManager->getCDKStatistics();
            echo "<div class='test-result test-success'>
                    <i class='fas fa-check-circle'></i> CDK统计: 总数 " . $stats['total_cdks'] . "，可用 " . $stats['available_cdks'] . "，已使用 " . $stats['used_cdks'] . "
                </div>";
        } else {
            echo "<div class='test-result test-error'>
                    <i class='fas fa-times-circle'></i> 生成CDK失败
                </div>";
        }
    }
    
} catch (Exception $e) {
    echo "<div class='test-result test-error'>
            <i class='fas fa-times-circle'></i> CDK管理测试失败: " . $e->getMessage() . "
        </div>";
}

echo "</div>";

// 测试订单管理
echo "<div class='test-section test-info'>
        <h3><i class='fas fa-shopping-cart'></i> 4. 订单管理测试</h3>";

try {
    $orderManager = new OrderManager();
    $productManager = new ProductManager();
    
    // 获取一个商品用于测试
    $products = $productManager->getAllProducts(true);
    if (empty($products)) {
        echo "<div class='test-result test-warning'>
                <i class='fas fa-exclamation-triangle'></i> 没有可用商品，跳过订单测试
            </div>";
    } else {
        $testProduct = $products[0];
        
        // 测试创建订单
        $orderResult = $orderManager->createOrder($testProduct['id'], $testProduct['price'], 'test@example.com', 'easypay', 0);
        if ($orderResult && isset($orderResult['order_no'])) {
            $orderNo = $orderResult['order_no'];
            echo "<div class='test-result test-success'>
                    <i class='fas fa-check-circle'></i> 创建订单成功，订单号: " . htmlspecialchars($orderNo) . "
                </div>";
            
            // 测试获取订单
            $order = $orderManager->getOrderByNo($orderNo);
            if ($order) {
                echo "<div class='test-result test-success'>
                        <i class='fas fa-check-circle'></i> 获取订单详情成功
                    </div>";
            }
            
            // 测试更新订单状态
            if ($orderManager->updateOrderStatus($order['id'], 'paid', $orderNo)) {
                echo "<div class='test-result test-success'>
                        <i class='fas fa-check-circle'></i> 更新订单状态为已支付成功
                    </div>";
            }
            
            // 测试获取订单列表
            $orders = $orderManager->getAllOrders();
            echo "<div class='test-result test-success'>
                    <i class='fas fa-check-circle'></i> 获取订单列表成功，共 " . count($orders) . " 个订单
                </div>";
            
            // 测试订单统计
            $stats = $orderManager->getOrderStatistics();
            echo "<div class='test-result test-success'>
                    <i class='fas fa-check-circle'></i> 订单统计: 总数 " . $stats['total_orders'] . "，待支付 " . $stats['pending_orders'] . "，已支付 " . $stats['completed_orders'] . "
                </div>";
        } else {
            echo "<div class='test-result test-error'>
                    <i class='fas fa-times-circle'></i> 创建订单失败
                </div>";
        }
    }
    
} catch (Exception $e) {
    echo "<div class='test-result test-error'>
            <i class='fas fa-times-circle'></i> 订单管理测试失败: " . $e->getMessage() . "
        </div>";
}

echo "</div>";

// 测试支付功能
echo "<div class='test-section test-info'>
        <h3><i class='fas fa-credit-card'></i> 5. 支付功能测试</h3>";

try {
    $easyPay = new EasyPay();
    
    // 测试创建支付请求
    $orderManager = new OrderManager();
    $products = $productManager->getAllProducts(true);
    
    if (!empty($products)) {
        $testProduct = $products[0];
        $orderResult = $orderManager->createOrder($testProduct['id'], $testProduct['price'], 'test@example.com', 'easypay', 0);
        if ($orderResult && isset($orderResult['order_no'])) {
            $orderNo = $orderResult['order_no'];
            $paymentResult = $easyPay->createPayment($orderNo, $testProduct['price'], 'alipay', $testProduct['name']);
            if ($paymentResult && isset($paymentResult['payment_url'])) {
                echo "<div class='test-result test-success'>
                        <i class='fas fa-check-circle'></i> 创建支付请求成功
                    </div>";
                echo "<div class='test-result test-info'>
                        <i class='fas fa-link'></i> 支付URL: <a href='" . htmlspecialchars($paymentResult['payment_url']) . "' target='_blank'>" . htmlspecialchars($paymentResult['payment_url']) . "</a>
                    </div>";
            } else {
                echo "<div class='test-result test-warning'>
                        <i class='fas fa-exclamation-triangle'></i> 创建支付请求失败（可能是配置问题）
                    </div>";
            }
        }
    }
    
} catch (Exception $e) {
    echo "<div class='test-result test-error'>
            <i class='fas fa-times-circle'></i> 支付功能测试失败: " . $e->getMessage() . "
        </div>";
}

echo "</div>";

// 测试系统设置
echo "<div class='test-section test-info'>
        <h3><i class='fas fa-cog'></i> 6. 系统设置测试</h3>";

try {
    // 测试获取网站名称
    $siteName = getSiteName();
    echo "<div class='test-result test-success'>
            <i class='fas fa-check-circle'></i> 获取网站名称成功: " . htmlspecialchars($siteName) . "
        </div>";
    
    // 测试获取系统设置
    $settingsHelper = SettingsHelper::getInstance();
    $settings = $settingsHelper->getAllSettings();
    echo "<div class='test-result test-success'>
            <i class='fas fa-check-circle'></i> 获取系统设置成功，共 " . count($settings) . " 个设置项
        </div>";
    
} catch (Exception $e) {
    echo "<div class='test-result test-error'>
            <i class='fas fa-times-circle'></i> 系统设置测试失败: " . $e->getMessage() . "
        </div>";
}

echo "</div>";

// 总结
echo "<div class='test-section test-info'>
        <h3><i class='fas fa-clipboard-check'></i> 测试总结</h3>
        <div class='alert alert-success'>
            <h4><i class='fas fa-check-circle'></i> 测试完成</h4>
            <p>所有核心功能测试已完成。如果所有测试都显示成功，说明系统运行正常。</p>
            <p>接下来您可以：</p>
            <ul>
                <li>访问 <a href='index.php' class='alert-link'>前台页面</a> 查看商品列表</li>
                <li>访问 <a href='admin/login.php' class='alert-link'>后台管理</a> 进行系统配置</li>
                <li>查看 <a href='README.md' class='alert-link'>使用指南</a> 了解更多功能</li>
            </ul>
        </div>
    </div>";

echo "</div>
</body>
</html>";
?>
